import Cookies from 'js-cookie'
// import axios from 'axios'
import {
	login_api_url
} from './setting.js'

import {
	Toast, Notify
} from 'vant'

export function LogoutCookie() {

	Cookies.remove('user_id')
	Cookies.remove('name')
	Cookies.remove('phone')
	Cookies.remove('token')
	Cookies.remove('duty')
	
	
};


//获取用户所有职务
export function SelectUserInfo(this_, user_id, method) {

	axios({
		method: 'GET',
		url: user_info,
		params: { //这里是发送给后台的数据
			user_id: user_id,
		}
	}).then((response) => { //这里使用了ES6的语法
		// console.log(response) //请求成功返回的数据
		if (response.data.status != 1) {

			this_.$message({
				message: response.data.errorMsg,
				type: 'error'
			})

			return
		}
		//获取结果后执行的函数
		method(response.data.resultBody)

	}).catch((error) => {
		// console.log(error) //请求失败返回的数据
		this_.$message({
			message: '请联系管理员' + '!!!',
			type: 'error'
		})
	})

}

//登录
export function Login(this_, phone, password) {

	// console.log(login_api_url)

	this_.$axios({
		method: 'POST',
		url: login_api_url,
		data: this_.qs.stringify({ //这里是发送给后台的数据
			phone: phone,
			password: password,
		})
	}).then((response) => { //这里使用了ES6的语法
		// console.log(response) //请求成功返回的数据
		if (response.data.status == 1) {
			// console.log('登录成功')
		
			
			Cookies.set('user_id', response.data.resultBody.user_info.id)
			Cookies.set('name', response.data.resultBody.user_info.name)
			Cookies.set('phone', response.data.resultBody.user_info.phone)
			Cookies.set('token', response.data.resultBody.token)
			Cookies.set('duty', response.data.resultBody.duty_info[0].duty_name)
			Cookies.set('duty_id', response.data.resultBody.duty_info[0].duty_id)
			
			this_.$router.push({
				'name': 'Home'
			})
			
			Notify({
				message: '登录成功' + '!!!',
				type: 'success'
			})
		} else {
			Notify({
				message: response.data.errorMsg + '!!!',
				type: 'danger'
			})
		}
	}).catch((error) => {
		// console.log(error) //请求失败返回的数据
		
		Notify({
			message: '请联系管理员' + '!!!',
			type: 'danger'
		})
	})
}
